<?php
namespace app\api\controller;
// use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
use think\captcha\Captcha;
use \app\api\model\Users as m;
class User extends Common
{
    public function index()
    {
        $db=Db::name("user");
        $query = input('get.query');//关键字
        $pagenum = input('get.pagenum');//当前页码
        $pagesize = input('get.pagesize');//当前显示行数

        $w['status'] = array('eq',1);
        if($query) $w['username'] = array('like','%'.$query.'%');
        $list = Db::name('user')->where($w)->limit(($pagenum - 1) * $pagesize,$pagesize)->select();
        // 总页数/行数
        $count = Db::name('user')->where($w)->count();
        $total = round($count/$pagesize);
        // dump($list);die();
        $arr['data']['pagenum']=$pagenum;
        $arr['data']['total']=$total;
        $arr['data']['users']=$list;
        $arr['meta']=array('msg'=>'获取用户列表成功','status'=>200);
        return json_encode($arr);
    }
    
    //删除
    public function del()
    {
        $id=input('id',0);
        $v=Db::name("user")->find($id);
//        print_r($v);
        if(Db::name("user")->delete($id)){
            $url="/admin/user/index";
//            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",$url);
        }else{
            $this->error("删除失败");
        }



    }


    public function action()
    {

        $act=$_POST['act'];
//        $sorting_arr=$_POST['sorting'];
        $checkid=$_POST['checkid'];
//        print_r($checkid);
//        print_r($_POST);

        // die;
        if($act=='sorting'){
            for($i=0;$i<count($checkid);$i++) {
                D()->execute("update __PREFIX__news set disorder=$sorting_arr[$i] where id='{$checkid[$i]}'");
                sys_log("信息排序更新:(id:$checkid[$i])");
            }
            $this->success("信息排序更新成功",cookie('Jumpurl'));
        }elseif($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("user")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }elseif($act=='isgood'||$act=='istop') {
            for($i=0;$i<count($checkid);$i++) {
                $v=Db::name("user")->find($checkid[$i]);
                $data[$act]=$v[$act]?0:1;
                Db::name("user")->where("id=".$checkid[$i])->update($data);
//                sys_log("删除信息:(id:$checkid[$i])");
            }
            $this->success("操作成功",cookie('Jumpurl'));
        }

    }

    //审核
    public function audit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("user")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("user")->where("id=".$id)->update($data)){
//            $this->success("审核成功","/admin/model/index.html");
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
////            $this->error("审核失败");
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }



    //订单管理
    public function order()
    {
        $pid=input("pid",0);
        $ty=input("ty",0);
        $tty=input("tty",0);
        $orderstatus=input("orderstatus",0);
        $pagesize=input('pagesize',0);


        $url="/admin/user/order";
        if($pid)$url.="/pid/".$pid;
        if($ty)$url.="/ty/".$ty;
        if($tty)$url.="/tty/".$tty;

        $model_fields=config('order_status');
//        print_r($model_fields);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        if($orderstatus)$map['orderstatus']=$orderstatus;
        $list=Db::name("order")->where($map)->order("sendtime desc,id desc")->paginate($pagesizes);


//        print_r($list);
        $this->assign("model_fields",$model_fields);
        $this->assign("pagesizes",$pagesizes);
        $this->assign("url",$url);
        $this->assign("orderstatus",$orderstatus);

        $this->assign("list",$list);

        return $this->fetch("/user/order");
    }

    //订单管理审核
    public function orderaudit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("order")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("order")->where("id=".$id)->update($data)){
//            $this->success("审核成功","/admin/model/index.html");
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
////            $this->error("审核失败");
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }
    //订单管理删除
    public function orderdel()
    {
        $id=input('id',0);
        $v=Db::name("order")->find($id);
//        print_r($v);
        if(Db::name("order")->delete($id)){
            $url="/admin/order/index";
//            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",cookie('Jumpurl'));
        }else{
            $this->error("删除失败");
        }
    }
    //订单管理全删
    public function orderaction()
    {
        $act=$_POST['act'];
        $checkid=$_POST['checkid'];
        if($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {

                $this->sys_log("删除信息:(id:$checkid[$i])");
//                echo $checkid[$i];
                Db::name("order")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }

    }
    //订单管理添加
    public function order_add()
    {
        $id=input("id",0);


        $v=Db::name("order")->find($id);
        if(Request::instance()->isPost()){
            print_r($_POST);
            unset($_POST['sendtime']);
                if(Db::name("order")->where("id={$id}")->update($_POST)){
                    $this->success("修改成功","/admin/user/order/pid/21/ty/46.html");

                }else{
                    $this->error("修改失败");
                }
        }


        $order_status=config('order_status');

//        print_r($v);

        $this->assign("order_status",$order_status);
        $this->assign("v",$v);


        return $this->fetch("/user/order_add");
    }
    //头部
    public function orderdata()
    {

        $id=$_POST['id'];
        $count=Db::name("order")->where("status=0")->count();//全部订单
        $count1=Db::name("order")->where("orderstatus=1 and status=0")->count();//全部订单

            $return_data['status'] = 0;
            $return_data['msg'] ="111";
            $return_data['count'] =$count;
//
        echo json_encode($return_data);


    }



    //我的行程
    public function itinerary()
    {
        $pid=input("pid",0);
        $ty=input("ty",0);
        $tty=input("tty",0);
        $orderstatus=input("orderstatus",0);
        $pagesize=input('pagesize',0);


        $url="/admin/user/itinerary";
        if($pid)$url.="/pid/".$pid;
        if($ty)$url.="/ty/".$ty;
        if($tty)$url.="/tty/".$tty;

        $model_fields=config('itinerary_status');
//        print_r($model_fields);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        if($orderstatus)$map['orderstatus']=$orderstatus;
        $list=Db::name("itinerary")->where($map)->order("sendtime desc,id desc")->paginate($pagesizes);


//        print_r($list);
        $this->assign("model_fields",$model_fields);
        $this->assign("pagesizes",$pagesizes);
        $this->assign("url",$url);
        $this->assign("orderstatus",$orderstatus);

        $this->assign("list",$list);

        return $this->fetch("/user/itinerary");
    }

    //我的行程审核
    public function itineraryaudit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("itinerary")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("itinerary")->where("id=".$id)->update($data)){
//            $this->success("审核成功","/admin/model/index.html");
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
////            $this->error("审核失败");
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }
    //我的行程删除
    public function itinerarydel()
    {
        $id=input('id',0);
        $v=Db::name("itinerary")->find($id);
//        print_r($v);
        if(Db::name("itinerary")->delete($id)){
//            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",cookie('Jumpurl'));
        }else{
            $this->error("删除失败");
        }
    }
    //我的行程全删
    public function itineraryaction()
    {
        $act=$_POST['act'];
        $checkid=$_POST['checkid'];
        if($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("itinerary")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }

    }
    //我的行程添加
    public function itinerary_add()
    {
        $id=input("id",0);
        $v=Db::name("order")->find($id);
        if(Request::instance()->isPost()){
//            print_r($_POST);
            unset($_POST['sendtime']);
            if(Db::name("itinerary")->update($_POST)){
                $this->success("修改成功","/admin/user/itinerary.html");

            }else{
                $this->error("修改失败");
            }
        }


        $order_status=config('itinerary_status');

//        print_r($v);

        $this->assign("order_status",$order_status);
        $this->assign("v",$v);


        return $this->fetch("/user/itinerary_add");
    }

}